package com.db.protocol.message;

import com.db.protocol.exception.NotParsingValueProtocolException;
import com.db.protocol.exception.ParseProtocolException;
import com.db.protocol.exception.TagMissingProtocolException;
import com.db.protocol.tags.MsgType;
import com.db.protocol.tags.OrderSide;
import com.db.protocol.tags.Tag;

import java.util.Map;

/**
 * @author nulls.narik
 */
public class ReportMessage extends AbstractMessage {

    public ReportMessage() {
        put(Tag.MSG_TYPE, MsgType.REPORT);
    }

    public ReportMessage(String clientID, String stockName, OrderSide side, Integer quantity, Float price, String exchangeID) {
        this();
        put(Tag.CLIENT_ID, clientID);
        put(Tag.TICKER_SYMBOL, stockName);
        put(Tag.ORDER_SIDE, side);
        put(Tag.QUANTITY, quantity);
        put(Tag.PRICE, price);
        put(Tag.EXCHANGE_ID, exchangeID);
    }

    public ReportMessage(Map<Tag, String> message) throws ParseProtocolException {
        this();

        if (!message.containsKey(Tag.CLIENT_ID)) {
            throw new TagMissingProtocolException(Tag.CLIENT_ID);
        }
        put(Tag.CLIENT_ID, message.get(Tag.CLIENT_ID));

        if (!message.containsKey(Tag.TICKER_SYMBOL)) {
            throw new TagMissingProtocolException(Tag.TICKER_SYMBOL);
        }
        put(Tag.TICKER_SYMBOL, message.get(Tag.TICKER_SYMBOL));

        if (!message.containsKey(Tag.ORDER_SIDE)) {
            throw new TagMissingProtocolException(Tag.ORDER_SIDE);
        }
        put(Tag.ORDER_SIDE, OrderSide.parse(message.get(Tag.ORDER_SIDE)));

        if (!message.containsKey(Tag.QUANTITY)) {
            throw new TagMissingProtocolException(Tag.QUANTITY);
        }
        try {
            put(Tag.QUANTITY, new Integer(message.get(Tag.QUANTITY)));
        } catch (NumberFormatException e) {
            throw new NotParsingValueProtocolException(Tag.QUANTITY);
        }

        if (!message.containsKey(Tag.PRICE)) {
            throw new TagMissingProtocolException(Tag.PRICE);
        }
        try {
            put(Tag.PRICE, new Float(message.get(Tag.PRICE)));
        } catch (NumberFormatException e) {
            throw new NotParsingValueProtocolException(Tag.PRICE);
        }

        if (!message.containsKey(Tag.EXCHANGE_ID)) {
            throw new TagMissingProtocolException(Tag.EXCHANGE_ID);
        }
        put(Tag.EXCHANGE_ID, message.get(Tag.EXCHANGE_ID));
    }

    @Override
    public String toString() {
        StringBuilder message = newMsg(MsgType.REPORT);

        message = addPair(message, Tag.CLIENT_ID, get(Tag.CLIENT_ID).toString());
        message = addPair(message, Tag.TICKER_SYMBOL, get(Tag.TICKER_SYMBOL).toString());
        message = addPair(message, Tag.ORDER_SIDE, String.valueOf(((OrderSide) get(Tag.ORDER_SIDE)).value()));
        message = addPair(message, Tag.QUANTITY, get(Tag.QUANTITY).toString());
        message = addPair(message, Tag.PRICE, get(Tag.PRICE).toString());
        message = addPair(message, Tag.EXCHANGE_ID, get(Tag.EXCHANGE_ID).toString());

        return message.toString();
    }

    public String getClientID() {
        return (String) get(Tag.CLIENT_ID);
    }

    public String getStockName() {
        return (String) get(Tag.TICKER_SYMBOL);
    }

    public OrderSide getOrderSide() {
        return (OrderSide) get(Tag.ORDER_SIDE);
    }

    public Integer getQuantity() {
        return (Integer) get(Tag.QUANTITY);
    }

    public Float getPrice() {
        return (Float) get(Tag.PRICE);
    }

    public String getExchangeID() {
        return (String) get(Tag.EXCHANGE_ID);
    }

}
